//
//  MKTChatItem_7_TVCell.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/14.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatItem_7_TVCell.h"
#import "MKTChatLink_LinkStyleCVCell.h"

@interface MKTChatItem_7_TVCell()<UICollectionViewDelegateFlowLayout>

@end

@implementation MKTChatItem_7_TVCell
+ (void)load{
    [self registerTableViewCellFor:[MKTChatItem_7 class]];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.buttonsView = [[MKUICollectionFlowLayoutView alloc] initWithFrame:self.bounds];
        self.buttonsView.collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.buttonsView.backgroundColor = [UIColor clearColor];
        self.buttonsView.bounces = NO;
        self.buttonsView.model.forwardDelegate = self;
        [self.contentView addSubview:self.buttonsView];
    }
    return self;
}

- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.buttonsView.frame = bounds;
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 60);
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    [self __reloadData];
}
- (void)__reloadData{
    MKTChatItem_7 *modelValue = self.cellModel.modelValue;
    [self.buttonsView.model removeAllSectionModels];
    for (MKTChatLink *link in modelValue.btnlist) {
        MKUICollectionViewCellModel *cm = [MKUICollectionViewCellModel modelWithValue:link cellClass:[MKTChatLink_LinkStyleCVCell class] whenClick:^(__kindof MKUICollectionViewCellModel * _Nonnull cellModel) {
            [link handleClick:cellModel.collectionViewCell];
        }];
        [self.buttonsView.model addCellModel:cm];
    }
    [self.buttonsView.model reloadCollectionViewData];
}
#pragma mark - delegate:UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGRect bounds = collectionView.bounds;
    CGSize s = bounds.size;
    NSInteger count = self.buttonsView.model.numberOfCells;
    CGFloat space = self.buttonsView.collectionViewFlowLayout.minimumInteritemSpacing;
    UIEdgeInsets insets = self.buttonsView.collectionViewFlowLayout.sectionInset;
    if(count){
        s.width = (bounds.size.width-insets.left-insets.right-(count-1)*space)/count;
    }
    s.height = 30;
    return s;
}
@end
